function GetDialogInstance(id, title, url, height, width,callback, istop, screenunit) {
    if (screenunit) {
        if(istop) {
            height = window.top.screen.height*height/100+"px";
            width = window.top.screen.width*width/100+"px";
        }else{
            height = window.screen.height*height/100+"px";
            width = window.screen.width*width/100+"px";
        }
    } else {
        height = height + 'px';
        width = width + 'px';
    }
    var template;
    var templatepath = GetRootPath() + 'fastdialog/fastdialog-template.html';
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", templatepath, false);
    xmlhttp.send();
    template = xmlhttp.responseText;
    DialoggConstructor = Vue.extend({
        template: template,
        data() {
            return {
                id: id,
                title: title,
                url: url,
                height: height,
                width: width
            }
        },
        methods: {
            close(id) {
                CloseDialogWithOutReturn(id,istop);

            },
            callbackevent(RetrunValue){
                if(callback){
                    if(RetrunValue){
                        callback(RetrunValue);
                    }else{
                        callback();
                    }
                    
                }
            }
        }
    });
    var dialoginstace = new DialoggConstructor();
    return dialoginstace;
}

function GetAlertInstance(dialogtype, title, message, callback, btnclosetext) {
    var id = uuid();
    var btntext = "关闭"
    if (btnclosetext) {
        btntext = btnclosetext;
    }
    var titleclass = "fast-modal-title";
    switch (dialogtype) {
        case "success":
            titleclass = "fast-modal-title-success";
            break;
        case "waring":
            titleclass = "fast-modal-title-waring";
            break;
        case "fail":
            titleclass = "fast-modal-title-fail";
            break;
    }
    var template;
    var templatepath = GetRootPath() + 'fastdialog/fastdialog-alert-template.html';
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", templatepath, false);
    xmlhttp.send();
    template = xmlhttp.responseText;
    DialoggConstructor = Vue.extend({
        template: template,
        data() {
            return {
                id: id,
                title: title,
                message: message,
                titleclass: titleclass,
                btnclosetext: btntext
            }
        },
        methods: {
            close(id) {
                document.getElementById(id).remove();
            },
            ok(id) {
                if (callback) {
                    callback();
                }
                document.getElementById(id).remove();
            },
        }
    });
    var dialoginstace = new DialoggConstructor();
    return dialoginstace;
}

function GetConfirmInstance(title, message, okcallback, btnoktext, btncanceltext) {
    var id = uuid();
    var oktext = "确认";
    var canceltext = "取消";
    if (btnoktext) {
        oktext = btnoktext;
    }
    if (btncanceltext) {
        canceltext = btncanceltext
    }
    var template;
    var templatepath = GetRootPath() + 'fastdialog/fastdialog-confirm-template.html';
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", templatepath, false);
    xmlhttp.send();
    template = xmlhttp.responseText;
    DialoggConstructor = Vue.extend({
        template: template,
        data() {
            return {
                id: id,
                title: title,
                message: message,
                btnoktext: oktext,
                btncanceltext: canceltext
            }
        },
        methods: {
            ok(id) {
                if (okcallback) {
                    okcallback();
                }
                document.getElementById(id).remove();
            },
            cancel(id) {
                document.getElementById(id).remove();
            },
            close(id) {
                document.getElementById(id).remove();
            }
        }
    });
    var dialoginstace = new DialoggConstructor();
    return dialoginstace;
}

function OpenDialog(id, title, url, height, width, callback, params, screenunit) {
    var dialog = GetDialogInstance(id, title, url, height, width,callback, false, screenunit);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
    if(window.dialoglist){
        console.log(window.dialoglist)
        for(var i=0;i<window.dialoglist.length;i++){
            if(window.dialoglist[i].id==id){
                window.dialoglist.splice(i,1);
                break;
            }
        }
        window.dialoglist.push({"id":id,"instance":dialog})
    }else{
        var dialoglist = [];
        dialoglist.push({"id":id,"instance":dialog});
        window.dialoglist = dialoglist;
    }
    if (params) {
        var iframe = window.document.getElementById(id + '_fastdialogiframe').contentWindow;
        iframe["fastdialog_params"] = params;
    }

}

function OpenTopDialog(id, title, url, height, width, callback, params, screenunit) {
    var dialog = GetDialogInstance(id, title, url, height, width,callback, true, screenunit);
    dialog.$mount();
    window.top.document.body.appendChild(dialog.$el);
    if(window.top.dialoglist){
        for(var i=0;i<window.top.dialoglist.length;i++){
            if(window.top.dialoglist[i].id==id){
                window.top.dialoglist.splice(i,1);
                break;
            }
        }
        window.top.dialoglist.push({"id":id,"instance":dialog})
    }else{
        var dialoglist = [];
        dialoglist.push({"id":id,"instance":dialog});
        window.top.dialoglist = dialoglist;
    }
    if (params) {
        var iframe = window.top.document.getElementById(id + '_fastdialogiframe').contentWindow;
        iframe["fastdialog_params"] = params;
    }
}

function CloseDialog(ReturnValue,id) {
    if(id){
        var obj = window.top.document.getElementById(id);
        if(obj){
            for(var i=0;i<window.top.dialoglist.length;i++){
                if(window.top.dialoglist[i].id==id){
                    if(ReturnValue){
                        window.top.dialoglist[i].instance.callbackevent(ReturnValue);
                    }else{
                        window.top.dialoglist[i].instance.callbackevent();
                    }
                    window.top.dialoglist.splice(i,1);
                    break;
                }
            }
            window.top.document.getElementById(id).remove();
         
        }else{
            if(ReturnValue){
                window.parent.dialoglist[0].instance.callbackevent(ReturnValue);
            }else{
                window.parent.dialoglist[0].instance.callbackevent();
            }
            window.parent.dialoglist.splice(0,window.parent.dialoglist.length);
            document.getElementById(id).remove();
           
        }
      
        
    }else{
        if(ReturnValue){
            window.parent.dialoglist[0].instance.callbackevent(ReturnValue);
        }else{
            window.parent.dialoglist[0].instance.callbackevent();
        }
       window.parent.dialoglist.splice(0,window.parent.dialoglist.length);
       var modallist =  window.parent.document.getElementsByClassName("fast-modal-bg");
       for(var i=0;i<modallist.length;i++){
           modallist[i].remove();
       }
       
    }
    
}

function CloseDialogWithOutReturn(id,istop) {
    if(id){
        if(istop){
            for(var i=0;i<window.top.dialoglist.length;i++){
                if(window.top.dialoglist[i].id==id){
                    window.top.dialoglist.splice(i,1);
                    break;
                }
            }
            window.top.document.getElementById(id).remove();
        }else{
            for(var i=0;i<window.parent.dialoglist.length;i++){
                if(window.parent.dialoglist[i].id==id){
                    window.parent.dialoglist.splice(i,1);
                    break;
                }
            }
            document.getElementById(id).remove();
        }        
    }
    
}


function GetParams() {
    if (window.fastdialog_params) {
        return window.fastdialog_params;
    } else {
        return "undefined";
    }
}

function OpenAlert(title, message, callback, btnclosetext) {
    var dialog = GetAlertInstance("alert", title, message, callback, btnclosetext);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
}

function OpenSuccess(title, message, callback, btnclosetext) {
    var dialog = GetAlertInstance("success", title, message, callback, btnclosetext);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
}

function OpenWaring(title, message, callback, btnclosetext) {
    var dialog = GetAlertInstance("waring", title, message, callback, btnclosetext);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
}

function OpenFail(title, message, callback, btnclosetext) {
    var dialog = GetAlertInstance("fail", title, message, callback, btnclosetext);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
}

function OpenConfirm(title, message, okcallback, btnoktext, btncanceltext) {
    var dialog = GetConfirmInstance(title, message, okcallback, btnoktext, btncanceltext);
    dialog.$mount();
    document.body.appendChild(dialog.$el);
}



function GetRootPath() {
    var loc = window.location,
        host = loc.hostname,
        protocol = loc.protocol,
        port = loc.port ? (':' + loc.port) : '';
    var path = location.pathname;

    if (path.indexOf('/') === 0) {
        path = path.substring(1);
    }

    var mypath = '/' + path.split('/')[0];
    path = (mypath != undefined ? mypath : ('/' + loc.pathname.split('/')[1])) + '/';

    var rootPath = protocol + '//' + host + port + path;
    return rootPath;
}

function uuid() {
    var date = new Date();
    return dateToString(date) + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function dateToString(now) {
    var year = now.getFullYear();
    var month = (now.getMonth() + 1).toString();
    var day = (now.getDate()).toString();
    var hour = (now.getHours()).toString();
    var minute = (now.getMinutes()).toString();
    var second = (now.getSeconds()).toString();
    if (month.length == 1) {
        month = "0" + month;
    }
    if (day.length == 1) {
        day = "0" + day;
    }
    if (hour.length == 1) {
        hour = "0" + hour;
    }
    if (minute.length == 1) {
        minute = "0" + minute;
    }
    if (second.length == 1) {
        second = "0" + second;
    }
    var dateTime = year + month + day + hour + minute + second;
    return dateTime;
}
